<?php
class App extends ModelBase
{
	private $tableName = 'myadmin_app';
	
	public static function getInstance()
	{
		return parent::instance(__CLASS__);
	}
	
	public function getAppInfo($aid)
	{
		$app = Yii::app()->db->createCommand()
		->select('*')
		->from($this->tableName)
		->where('app_id=:app_id', array(':app_id'=>$aid))
		->queryRow();
		return $app;
	}
	
	public function getOnlineApp($status = 1)
	{
		$app = Yii::app()->db->createCommand()->select('*')->from($this->tableName);
		if ($status)$app->where('status = 1');
		$app = $app->queryAll();
		return $app;
	}
	
	/**
	 * 获取用户可见app
	 * @param string $user_role
	 * @return array
	 */
	public function getAppByMenuIds($user_role)
	{
		$whereStr =  $_SESSION['uid'] == 1 ? '' : 'and  a.menu_id in('.$user_role.')';
		$menu = Yii::app()->db->createCommand()
		->select('c.*')
		->from('myadmin_menu_url a')
		->leftJoin('myadmin_module b', 'b.module_id = a.module_id')
		->leftJoin('myadmin_app c', 'c.app_id = b.app_id')
		->where('a.is_show  = 1 and a.online =1 '.$whereStr.' group by c.app_name order by c.app_id asc ') 
		->queryAll();

		return $menu;
	}
	
	public function addApp($data)
	{
		$ret = Yii::app()->db->createCommand()->insert($this->tableName,$data);
		return $ret;
	}
	
	
	public function modifyApp($data,$aid)
	{	
		$aid = (int) $aid;
		if(!$aid)
		{
			return false;
		}
		$ret = Yii::app()->db->createCommand()->update($this->tableName,$data,'app_id = '.$aid);
		return $ret;
	}
	
	public function delApp($appId)
	{
		$appId = (int) $appId;
		if(!$appId)
		{
			return false;
		}
		$ret = Yii::app()->db->createCommand()->delete($this->tableName,'app_id='.$appId);
		return $ret;
	}
	
}